package cn.example.handlertest2;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_star,btn_child;

    private Handler myhandler;

    private Handler mainHandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d("主线程", "handleMessage: "+msg);
            Message msg2 = new Message();
            msg.what=1;
            myhandler.sendMessageDelayed(msg2,1000);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_star=findViewById(R.id.btn_star);
        btn_child=findViewById(R.id.btn_child);
        btn_star.setOnClickListener(this);
        btn_child.setOnClickListener(this);

        HandlerThread handlerThread=new HandlerThread("handler thread");
        handlerThread.start();
        myhandler=new Handler(handlerThread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Message message = new Message();
                mainHandler.sendMessageDelayed(message,1000);
                Log.d("字线程", "handleMessage: "+msg);
            }
        };

       
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_star:
                mainHandler.sendEmptyMessage(1);
                break;
            case R.id.btn_child:
                mainHandler.removeMessages(1);
                break;
        }
    }
}
